#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

public Plugin myinfo = 
{
    name = "l4d2_teleport_to_aime",
    author = "豆瓣酱な",
    description = "管理员使用 !bp 命令传送到瞄准点.",
    version = "1.0",
    url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
};


public void OnPluginStart()
{
    RegAdminCmd("sm_bp", Command_TeleportToAim, ADMFLAG_GENERIC, "传送到你瞄准的位置");
}

public Action Command_TeleportToAim(int client, int args)
{
    if (!client)
    {
        ReplyToCommand(client, "该命令只能在游戏中由玩家使用。");
        return Plugin_Handled;
    }
    
    if (!IsPlayerAlive(client))
    {
        ReplyToCommand(client, "你必须是活着的玩家才能使用此命令。");
        return Plugin_Handled;
    }
    
    float eyePos[3];
    float eyeAng[3];
    float endPos[3];
    
    GetClientEyePosition(client, eyePos);
    GetClientEyeAngles(client, eyeAng);
    
    Handle trace = TR_TraceRayFilterEx(eyePos, eyeAng, MASK_ALL, RayType_Infinite, TraceEntityFilterPlayer, client);
    
    if (TR_DidHit(trace))
    {
        TR_GetEndPosition(endPos, trace);
        
        // 稍微抬高一点位置，避免卡在地面里
        endPos[2] += 5.0;
        
        TeleportEntity(client, endPos, NULL_VECTOR, NULL_VECTOR);
        PrintToChat(client, "\x04[传送]\x01 你已传送到瞄准的位置。");
    }
    else
    {
        PrintToChat(client, "\x04[传送]\x01 未能找到有效的瞄准点。");
    }
    
    CloseHandle(trace);
    return Plugin_Handled;
}

public bool TraceEntityFilterPlayer(int entity, int contentsMask, any client)
{
    return entity != client;
}